import java.math.BigInteger;

// 972
public class P56 {
	public static void main(String[] args) {
		int max = 0;
		for (int i = 1; i < 100; i++) {
			for (int j = 1; j < 100; j++) {
				BigInteger x = new BigInteger(String.valueOf(i)).pow(j);
				String num = x.toString();
				int sum = 0;
				
				for (int n = 0; n < num.length(); n++) {
					sum += Integer.parseInt(num.substring(n, n + 1));
				}
				
				if (max < sum) {
					max = sum;
				}
			}
		}
		
		System.out.println(max);
	}
}
